package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.payment.feign.GmallOmsClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.pojo.PaymentInfoEntity;
import com.atguigu.gmall.payment.service.PaymentService;
import com.atguigu.gmall.payment.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private GmallOmsClient omsClient;

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Override
    public OrderEntity queryOrderByToken(String orderToken) {
        ResponseVo<OrderEntity> orderEntityResponseVo = omsClient.queryOrderByToken(orderToken);
        OrderEntity orderEntity = orderEntityResponseVo.getData();
        return orderEntity;
    }

    //保存对账信息
    @Override
    public Long savePaymentInfo(PayVo payVo) {
        PaymentInfoEntity paymentInfoEntity = new PaymentInfoEntity();
        paymentInfoEntity.setOutTradeNo(payVo.getOut_trade_no());
        paymentInfoEntity.setPaymentType(1);
        paymentInfoEntity.setTotalAmount(new BigDecimal(payVo.getTotal_amount()));
        paymentInfoEntity.setSubject(payVo.getSubject());
        paymentInfoEntity.setPaymentStatus(0);
        paymentInfoEntity.setCreateTime(new Date());
        paymentInfoMapper.insert(paymentInfoEntity);
        return paymentInfoEntity.getId();
    }

    //根据对账记录id查询对账记录
    @Override
    public PaymentInfoEntity queryPatmentInfo(String payId) {
        PaymentInfoEntity paymentInfoEntity = paymentInfoMapper.selectById(payId);
        return paymentInfoEntity;
    }

    //更新对账记录
    @Override
    public Integer updatePaymentInfo(PaymentInfoEntity paymentInfoEntity) {
        int inferenceRow = paymentInfoMapper.updateById(paymentInfoEntity);
        return inferenceRow;
    }
}
